package MTRequest;

import java.sql.ResultSet;

import MTProxyServer.MTConstant;
import MTProxyServer.MessageCentre;
import SQLManager.SQLManager;

/**
 * This Request is handling the client to register a new account
 * @author King
 *
 */
public class MTServerRegisterRequest extends MTRequest {

	public MTServerRegisterRequest( MessageCentre aMSGCenter )
	{
		super( aMSGCenter );
		this.mRequestState = 0;
	}
	
	protected void ProcessReqest( int aPeerState, String peerRawData )
	{
		switch( aPeerState ){
			case MTConstant.MT_REQ_CREG_SEND_PHONENUM:{
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				//---- Check Phone available
				
				boolean isAvailable = true;
				SQLManager sqlManager = SQLManager.GetInstance();
				try{
					sqlManager.ConnectSQL( );
					
					ResultSet rs = sqlManager.Query( 
							String.format("SELECT * FROM " + MTConstant.MT_SQL_DB + ".UserTable where PhoneNumber = '%s'", peerRawData )
							, "test" );
					
					if( rs.next() ){
						isAvailable = false;
						
						System.out.println( "UserID: " + rs.getString( "UserID" ));
						System.out.println( "UserName: " + rs.getString( "UserName" ));
						System.out.println( "PhoneNumer: " + rs.getString( "PhoneNumber" ));
					}
					sqlManager.Close();
				}
				catch( Exception e ){
					e.printStackTrace();
				}
				
				this.mSendingToPeerRawData.add( "" + isAvailable );
				break;
			}
			case MTConstant.MT_REQ_CREG_SEND_USER_INFO:{
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				for( int i = 0 ; i < this.mReceivedFromPeerRawData.size() ; ++i ){
					System.out.println("User Data " + i + ": "+ this.mReceivedFromPeerRawData.get( i ));
				}
				
				// Create a new user
				// and Update DB
				
				int userID = 0;
				SQLManager sqlManager = SQLManager.GetInstance();
				
				try{
					sqlManager.ConnectSQL( );
					
					String query = String.format(
							"INSERT INTO " + MTConstant.MT_SQL_DB + ".UserTable ( UserName, PhoneNumber ) VALUES ( '%s', '%s')", 
							this.mReceivedFromPeerRawData.get( 1 ),
							this.mReceivedFromPeerRawData.get( 0 ) );
					
					sqlManager.UpdateTable( query );
					
					query = "SELECT LAST_INSERT_ID()";
					ResultSet rs = sqlManager.Query( query, "");
					
					
					if( rs.next() ){
						userID = rs.getInt( 1 );
					}
					
					// Create A new User DB
					String userDB = "User_" + userID;
					
					// Create a Contact List Table
					query = String.format(  
							"CREATE TABLE %s.%s_ContactListTable ( " +
							"UserID INT NOT NULL ," +
							"PRIMARY KEY ( UserID ) )"
							, MTConstant.MT_SQL_DB, userDB ); 
					sqlManager.UpdateTable( query );
					
					// Create a new User's Update Table
					query = String.format(
							"CREATE  TABLE %s.%s_UpdateTable (" +
							"ID INT NOT NULL AUTO_INCREMENT," +
							"EventID INT NOT NULL ," +
							"Master INT NULL ," +
							"Content VARCHAR(255) NULL ," +
							"IsFinished TINYINT(1)  NULL ," +
							"UpdateType INT NULL ," +
							"TimeCreated DATETIME NULL ," +
							"PRIMARY KEY (ID),  UNIQUE INDEX ID_UNIQUE ( ID ASC) )"
							, MTConstant.MT_SQL_DB, userDB );
					sqlManager.UpdateTable( query );
					
					sqlManager.Close();
				}
				catch( Exception e ){
					e.printStackTrace();
				}
				
				this.mSendingToPeerRawData.add( "" + userID );
				break;
			}
		}
	}
	
	protected void UpdateRequestState( int aPeerState )
	{
		this.mRequestState = aPeerState;
	}
	
	public void SendRequest()
	{
		boolean isClose = false;
		String request = "" + MTConstant.MT_REQID_REIGSTER + MTConstant.MT_REQ_ITEM_SEPARATER
						+ this.mRequestState + MTConstant.MT_REQ_ITEM_SEPARATER;
		switch( this.mRequestState ){
			case MTConstant.MT_REQ_SREG_CHECK_PHONENUM:{
				request += this.mSendingToPeerRawData.get( 0 );
				
				boolean isAvailable = Boolean.valueOf( mSendingToPeerRawData.get(0) );
				if( false == isAvailable ){
					isClose = true;
				}
				break;
			}
			case MTConstant.MT_REQ_SREG_UPDATE_USER_INFO:{
				request += this.mSendingToPeerRawData.get( 1 );
				isClose = true;
				break;
			}
		}
		this.SendMSG(request);
		
		if( true == isClose ){
			this.Finish();
		}
	}
}
